﻿using Simple.Common.EventBus;
using Simple.Common.Models;

namespace Simple.Services.EventHandlers
{
    public class LoginEventHandler : IEventHandler<LoginEvent>
    {
        private readonly SimpleDbContext _context;

        public LoginEventHandler(SimpleDbContext context)
        {
            _context = context;
        }

        public async Task Handle(LoginEvent @event)
        {
            SysLoginLogs loginLog = new SysLoginLogs()
            {
                LoginSource = @event.LoginSource,
                LoginType = @event.LoginType,
                IpArea = RequestHelper.GetAddressByIp(@event.Ip),
                Account = @event.Account,
                Result = @event.Result,
                IsSuccess = @event.IsSuccess,
                Browser = @event.Browser,
                OperatingSystem = @event.OperatingSystem,
                Ip = @event.Ip,
                ElapsedTime = @event.ElapsedTime,
                OperatingTime = @event.OperatingTime
            };

            _context.Add(loginLog);
            await _context.SaveChangesAsync();
        }
    }
}
